﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using PagedList;
using PagedList.Mvc;
using Project_2._2.Extra;
using System.Web.Security;

namespace Project_2._2.Controllers
{
    public class AdminFeedbackController : Controller
    {
        //
        // GET: /Feedback/
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var qc = (from p in db.Feedbacks where  p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                        ViewBag.record = rd;
                        return View(qc);
                    }
                    else
                    {
                        var qc = (from p in db.Feedbacks where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, 5);
                        return View(qc);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            } 
            //var fb = (from p in db.Feedbacks where p.isDelete == false select p).ToList();
            //return View(fb.ToList().ToPagedList(page ?? 1, 7));

        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            try
            {
                int record = 0;
                if (string.IsNullOrEmpty(col["KeyWord"]))
                {
                    record = int.Parse(col["Record"]);
                    var feedback = (from p in db.Feedbacks where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                    ViewBag.record = col["Record"];
                    return View(feedback);
                }
                else
                {
                    string SearchBy = col["SearchBy"].ToString();
                    string key = col["KeyWord"].ToString();

                    switch (SearchBy)
                    {
                        case "HoTen":
                            var feedback = (from p in db.Feedbacks where p.isDelete == false & p.HoTen.Contains(key) select p).ToList().ToPagedList(page ?? 1, 10);
                            ViewBag.record = "1";
                            return View(feedback); break;
                        case "Email":
                            feedback = (from p in db.Feedbacks where p.isDelete == false & p.Email.Contains(key) select p).ToList().ToPagedList(page ?? 1, 10);
                            ViewBag.record = "1";
                            return View(feedback); break;
                        case "TieuDe":
                            feedback = (from p in db.Feedbacks where p.isDelete == false & p.TieuDe.Contains(key) select p).ToList().ToPagedList(page ?? 1, 10);
                            ViewBag.record = "1";
                            return View(feedback); break;
                        case "NoiDung":
                            feedback = (from p in db.Feedbacks where p.isDelete == false & p.NoiDung.Contains(key) select p).ToList().ToPagedList(page ?? 1, 10);
                            ViewBag.record = "1";
                            return View(feedback); break;
                    }
                    return View();
                }
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }            
        }

        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> feedbackIdsToDelete)
        {
            try
            {
                var fb_delete = db.Feedbacks.Where(x => feedbackIdsToDelete.Contains(x.FeedbackId)).ToList();
                foreach (var fb in fb_delete)
                {
                    fb.isDelete = true;
                    UpdateModel(fb);
                    db.SaveChanges();
                    //db.Feedbacks.Remove(fb);
                }
                //db.SaveChanges();
                return RedirectToAction("Default", "AdminFeedback");
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }            
        }

        public ActionResult Accept(int id)
        {
            try
            {
                Feedback fb = (from p in db.Feedbacks where p.FeedbackId == id select p).First();
                fb.TrangThai = true;
                fb.isDelete = true;
                UpdateModel(fb);
                db.SaveChanges();
                return RedirectToAction("Default", "AdminFeedback");
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }            
        }

        public ActionResult Delete(int id)
        {
            try
            {
                Feedback fb = (from p in db.Feedbacks where p.FeedbackId == id select p).First();
                fb.isDelete = true;
                UpdateModel(fb);
                db.SaveChanges();
                return RedirectToAction("Default", "AdminFeedback");
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }           
        }

        public ActionResult Search()
        {
            return View();
        }

    }
}
